package com.gm.wholesale.module.order.presenter;

import android.content.Context;
import android.util.ArrayMap;

import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.module.custom.ChoiceCustomData;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.good.bean.GoodBean;
import com.gm.wholesale.module.order.bean.CreateOrderBean;
import com.gm.wholesale.module.order.bean.OrderDetailBean;
import com.gm.wholesale.module.order.bean.OrderPayInfoBean;
import com.gm.wholesale.module.order.model.OrderModel;
import com.gm.wholesale.module.order.view.PayMoneyView;

import java.util.ArrayList;
import java.util.List;

public class CreateOrderPresenter {

    /**
     * @param context
     * @param orderDetailBean
     * @param payInfoBean
     * @param listener
     */

    //订单里面的 收款
    public static void subMitCollection(Context context, OrderDetailBean orderDetailBean, OrderPayInfoBean payInfoBean,
                                              PayMoneyView.OnCreateOrderListener listener) {
        long orderId=orderDetailBean.getOrderInfo().getOrderId();
        ArrayMap<String,Object> data=new ArrayMap<>();
        data.put("payment",payInfoBean.getPayment());
        data.put("payAmount", Double.parseDouble(payInfoBean.getPaidAmount()));
        data.put("discountAmount",Double.parseDouble(payInfoBean.getDiscountAmount()));
        OrderModel.payOrder((IBaseView) context, orderId,data, new BaseHttpObserver<BaseResp>() {
            @Override
            protected void handleResultOkResp(BaseResp resp) {
                listener.onCreateSuccess();
            }
        });



    }



    public static void subMitOrder(Context context, List<GoodBean> mGoodBeans,
                                   PayMoneyView.OnCreateOrderListener listener) {
        CustomBean customBean = ChoiceCustomData.newInstance().getCustomBean();
        if (customBean == null) {
            ToastUtil.showToast("请选择客户");
            return;
        }
        CreateOrderBean createOrderBean = new CreateOrderBean();
        createOrderBean.setCustomerName(customBean.getName());
        List<CreateOrderBean.DetailsDTO> detailsDTOS = new ArrayList<>();
        for (GoodBean goodBean : mGoodBeans) {
            CreateOrderBean.DetailsDTO detailsDTO = new CreateOrderBean.DetailsDTO();
            detailsDTO.setName(goodBean.getName());
            detailsDTO.setUnitPrice(goodBean.getPrice() + "");
            detailsDTO.setOutgoingUnit(goodBean.getUnitName());
            detailsDTO.setOutgoingQuantity(goodBean.getGoodNum() + "");
            detailsDTOS.add(detailsDTO);
        }
        createOrderBean.setDetails(detailsDTOS);
        OrderModel.createOrder((IBaseView) context, createOrderBean, new BaseHttpObserver<BaseResp>() {
            @Override
            protected void handleResultOkResp(BaseResp resp) {
                if (listener != null) {
                    listener.onCreateSuccess();
                }
                ToastUtil.showToast("提交成功");
                Popupwindow.hiddenPop();
            }
        });


    }


    public static void updateOrder(Context context,long orderId,long version, List<GoodBean> mGoodBeans,
                                   PayMoneyView.OnCreateOrderListener listener) {
        CustomBean customBean = ChoiceCustomData.newInstance().getCustomBean();
        if (customBean == null) {
            ToastUtil.showToast("请选择客户");
            return;
        }
        CreateOrderBean createOrderBean = new CreateOrderBean();
        createOrderBean.setCustomerName(customBean.getName());
        createOrderBean.setVersion(version);
        List<CreateOrderBean.DetailsDTO> detailsDTOS = new ArrayList<>();
        for (GoodBean goodBean : mGoodBeans) {
            CreateOrderBean.DetailsDTO detailsDTO = new CreateOrderBean.DetailsDTO();
            detailsDTO.setName(goodBean.getName());
            detailsDTO.setUnitPrice(goodBean.getPrice() + "");
            detailsDTO.setOutgoingUnit(goodBean.getUnitName());
            if (goodBean.getOrderLineId()!=null){
                detailsDTO.setOrderLineId(goodBean.getOrderLineId());
            }
            detailsDTO.setOutgoingQuantity(goodBean.getGoodNum() + "");
            detailsDTOS.add(detailsDTO);
        }
        createOrderBean.setDetails(detailsDTOS);
        OrderModel.updateOrder((IBaseView) context,orderId, createOrderBean, new BaseHttpObserver<BaseResp>() {
            @Override
            protected void handleResultOkResp(BaseResp resp) {
                if (listener != null) {
                    listener.onCreateSuccess();
                }
                ToastUtil.showToast("操作成功");
                Popupwindow.hiddenPop();
            }
        });


    }



}
